import axios from '@/libs/api.request'
import {params} from '@/libs/page'

class CRUD {
  constructor (baseUrl = baseURL) {
    this.baseUrl = baseUrl
  }

  list (page) {
    return axios.post(this.baseUrl + '/list', params(page))
  }

  create (entity) {
    return axios.post(this.baseUrl + '/create', entity)
  }

  update (entity) {
    return axios.post(this.baseUrl + '/' + entity.id + '/update', entity)
  }

  save (entity) {
    if (entity.id) {
      return this.update(entity)
    } else {
      return this.create(entity)
    }
  }

  delete (id) {
    return axios.get(this.baseUrl + '/' + id + '/delete')
  }

  deleteInBatch (ids) {
    return axios.post(this.baseUrl + '/batch/delete', { ids })
  }
}

export default CRUD
